package com.sdkj.dspservice.modules.analysis.entity;

import com.sdkj.dspservice.common.utils.IdGen;
import com.sdkj.dspservice.common.utils.SpringContextHolder;
import com.sdkj.dspservice.common.utils.StringUtils;
import lombok.Data;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * url统计
 */
@Data
public class PageAnalysis {

    /**
     * 标题
     */
    private String title;
    /**
     * 标题列表
     */
    private List<String> titleList;
    /**
     * 地址
     */
    private String url;
    /**
     * 参数
     */
    private String query;
    /**
     * 日期
     */
    private String date;
    /**
     * 地市名
     */
    private String cityName;
    /**
     * pv
     */
    private Integer pv;
    /**
     * uv
     */
    private Integer uv;
    /**
     * 查询条件：开始日期
     */
    private String startDate;
    /**
     * 查询条件：结束日期
     */
    private String endDate;
    /**
     * 分页：当前页码
     */
    private Integer current;
    /**
     * 分页：页面数据条数
     */
    private Integer size;
    private Integer limitLeft;

    private Integer[] cityPv;
    private Integer[] cityUv;

    private String key = IdGen.uuid();

    private List<String> searchUrlList;
    /**
     * 是否查询参数合并
     */
    private String isSearchParamMerge;
    /**
     * 是否合并
     */
    private String isMerge;

    /**
     * 日期条件传入为一个数组，这里给它分配到startDate、endDate上
     *
     * @param dateRange
     */
    public void setDateRange(String[] dateRange) {
        // 获取运行模式
        String springProfilesActive = SpringContextHolder.getApplicationContext().getEnvironment().getActiveProfiles()[0];
        String startDate = dateRange[0];
        String endDate = dateRange[1];
        if (startDate.length() > 10) {
            startDate = startDate.substring(0, 10);
            // 时区问题+1天
            startDate = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        }
        if (endDate.length() > 10) {
            endDate = endDate.substring(0, 10);
            // 时区问题+1天
            endDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        }
        this.startDate = startDate;
        this.endDate = endDate;
    }

    public void setCityPvUvByList(List<PageAnalysis> pageAnalysisList) {
        Integer[] cityUvs = new Integer[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        Integer[] cityPvs = new Integer[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (PageAnalysis analysis : pageAnalysisList) {
            switch (analysis.getCityName()) {
                case "鹰潭":
                    cityUvs[0] = analysis.getUv();
                    cityPvs[0] = analysis.getPv();
                    break;
                case "新余":
                    cityUvs[1] = analysis.getUv();
                    cityPvs[1] = analysis.getPv();
                    break;
                case "南昌":
                    cityUvs[2] = analysis.getUv();
                    cityPvs[2] = analysis.getPv();
                    break;
                case "九江":
                    cityUvs[3] = analysis.getUv();
                    cityPvs[3] = analysis.getPv();
                    break;
                case "上饶":
                    cityUvs[4] = analysis.getUv();
                    cityPvs[4] = analysis.getPv();
                    break;
                case "抚州":
                    cityUvs[5] = analysis.getUv();
                    cityPvs[5] = analysis.getPv();
                    break;
                case "宜春":
                    cityUvs[6] = analysis.getUv();
                    cityPvs[6] = analysis.getPv();
                    break;
                case "吉安":
                    cityUvs[7] = analysis.getUv();
                    cityPvs[7] = analysis.getPv();
                    break;
                case "赣州":
                    cityUvs[8] = analysis.getUv();
                    cityPvs[8] = analysis.getPv();
                    break;
                case "景德镇":
                    cityUvs[9] = analysis.getUv();
                    cityPvs[9] = analysis.getPv();
                    break;
                case "萍乡":
                    cityUvs[10] = analysis.getUv();
                    cityPvs[10] = analysis.getPv();
                    break;
            }
        }
        cityPv = cityPvs;
        cityUv = cityUvs;
    }

    public void setSearchUrlList(List<String> searchUrlList) {
        this.searchUrlList = searchUrlList;
        // 查看是否查询参数合并
        if (searchUrlList != null && !searchUrlList.isEmpty()) {
            this.isSearchParamMerge = searchUrlList.stream().filter(s -> s.indexOf("参数合并") != -1).count() > 0 ? "1" : "0";
        }
    }

    public int getDateNumber() {
        return StringUtils.toInteger(date);
    }
}
